(function($) {
	
	//var isSale = true;
	var isMap='n';
//	var strWebSite = "http://127.0.0.1/~Lance/Realty/Realty/";
	var strWebSite = "http://23.21.84.9/Realty_iPad/";
//	var strWebSite = "http://daria/@thetigerparty/RealtyApp/svn/Realty_iPad/";
	
	
	$(document).ready(function(e){
		SearchAgent(e);
//		AdvanceSearch(e);
		$('.ListSearchAgent').click(SearchAgentBtn);	// Button or Search Bar for Search List
		$('#searchinput1').keyup(SearchAgent);			// Button or Search Bar for Search List
//		$('#searchinput1').keydown(SearchAgent);		// Button or Search Bar for Search List
//		$('#searchinput1').change(SearchAgent);			// Button or Search Bar for Search List
							
//		$('.AgentDetailBtn').click(AgentDetail);
//		$('.for-sale').click( UIClickSale );
//		$('.for-rent').click( UIClickRent );

		$('#BtAdvancedSearch').click(BuildAdvanceSearch);
		$('#selectmenu4').change(BuildAdvanceSearchCity);
		$('#selectmenu5').change(BuildAdvanceSearchBorough);
		
		$('.ListLocationSearch').click(LocationSearch);
		$('.ListAdvanceSearch').click(AdvanceSearch);

		$('#P10PreBtn').click(PreNextBtn); 
		$('#P10NextBtn').click(PreNextBtn); 

		$('.MapResults').click(ViewMapResults); 
		
		$('#BrokerMap').click(ViewBrokerMap);
		
		//daria
		$("#radioShowType2").click(function(){
			isMap='n';
			$('#resultmap').hide();
			$('#resultmapspace').hide();
			searchSpaceSize();
		});
		$("#radioShowType1").click(function(){
			isMap='y';
			$('#resultmap').show();
			$('#resultmapspace').show();
			searchSpaceSize();
		});
		
		$("#page6radioShowType2").click(function(){
			isMap='n';
			$('#page6resultmap').hide();
			$('#page6resultmapspace').hide();
			searchSpaceSize();
		});
		$("#page6radioShowType1").click(function(){
			isMap='y';
			$('#page6resultmap').show();
			$('#page6resultmapspace').show();
			searchSpaceSize();
		});
		$("#page6radio1").click(function(){
			$('#page6resultmap').hide();
			$('#page6resultmapspace').hide();
			$('#page6resultlist').hide();
			$('#page6resultSearches').show();
			
			$("#page6radioShowType1").addClass('ui-disabled');
			$("#page6radioShowType2").addClass('ui-disabled');
		});
		$("#page6radio2").click(function(){
			$('#page6resultmap').show();
			$('#page6resultmapspace').show();
			$('#page6resultlist').show();
			$('#page6resultSearches').hide();
			
			$("#page6radioShowType1").removeClass('ui-disabled');
			$("#page6radioShowType2").removeClass('ui-disabled');
		});
		
//		$('.ToPage14').click(ViewDetail);
	});
	
	function SearchAgentBtn(e)
	{
/*		switch( $(this).attr("id") )
		{
			case "Page1ToPage8":
				$('#Page4Back').attr( "href", "#page1");
				break;
			case "Page5ToPage4":
				$('#Page4Back').attr( "href", "#page5");
				break;
			case "Page7ToPage4":
				$('#Page4Back').attr( "href", "#page7");
				break;
		}
*/		SearchAgent(e, $(this).attr("id") );
	}

	function SearchAgent(e, CallerID)
	{
		var strAgent = $("#searchinput1").val();
		if( CallerID != null )
		{
			var ID = CallerID;
		}
		else
		{
			var ID = 0;
		}
		$.post( strWebSite + 'SearchAgent1.php',
			{"SelectKey": strAgent,
		 	"Href": ID},
			function(data){
			$("#SearchAgentList").html(data);
			//alert($("#SearchAgentList").html());
			$('.AgentSelect').click(AgentListBtn);		// Search Button To Page 6 (Main Page) or Page 7( More Page- Change Agent ) 		
			$("#SearchAgentList").trigger("create");

		},'html');
	}

	function AgentListBtn(e)
	{
		var AgentID = $(this).attr("id");
		$.post( strWebSite + 'SelectAgent.php',
			{"AgentID": AgentID},
			function(data){
				$("#ViewAgentDetail").html(data);
			});
		$.post( strWebSite + 'SelectBroker.php',
			function(data){
				$("#ViewBrokerDetail").html(data);
			});
	}
/*
	function UIClickSale (e)
	{
		$('.for-sale').addClass("ui-btn-active");
		$('.for-rent').removeClass("ui-btn-active");
		isSale = true;	
	}
	function UIClickRent (e)
	{
		$('.for-sale').removeClass("ui-btn-active");
		$('.for-rent').addClass("ui-btn-active");
		isSale = false;
	}
*/
	function BuildAdvanceSearch(e)
	{
		$.post(strWebSite + 'ListSelectCity.php',
			function(data)
			{
				$("#selectmenu4").html(data);
				$('select#selectmenu4').trigger("change");
//				$("#selectmenu4").text("Any");
			}
		);
		BuildAdvanceSearchCity(e);
	}
	function BuildAdvanceSearchCity(e)
	{
		$.post(strWebSite + 'ListSelectBorough.php',
			{"City": $("#selectmenu4 :selected").text()},
			function(data){
				$("#selectmenu5").html(data);
				$('select#selectmenu5').trigger("change");
//				$("#selectmenu5").text("Any");			
			}
		);
		BuildAdvanceSearchBorough(e);
	}
	function BuildAdvanceSearchBorough(e)
	{
		$.post(strWebSite + 'ListSelectPrice.php',
			{"City": $("#selectmenu4 :selected").text(),
			"Borough": $("#selectmenu5 :selected").text()},
			function(data){
//				$("#test").html(data);
				$("#selectmenu1").html(data);
				$('select#selectmenu1').trigger("change");
//				$("#selectmenu1").text("Any");
			}
		);
	}
				
	function LocationSearch(e)
	{
		var strLocation = $("#searchinput3").val();
		$.post( strWebSite + 'SearchProperties.php',
		{"strLocation": strLocation,
		"isSale": $('#SearchSlae').hasClass("ui-btn-active")},
		function(data){
			$("#ListResult").html(data);
			$("#ListResult").trigger("create");
			$('.ViewDetailBlock').click(ViewDetail); 
		});
	}
	
	function AdvanceSearch(e){
		var City = $("#selectmenu4 :selected").text();
		var Neighbor = $("#selectmenu5 :selected").text();
		//var Neighbor = $("#selectmenu6 :selected").text();
		var Price = $("#selectmenu1 :selected").val();
		var Bedrooms = $("#selectmenu2 :selected").text();
		var Bathrooms = $("#selectmenu3 :selected").text();
		
		$.post(strWebSite + 'AdvSearch.php',
		{"City": City,
//		"Borough": Borough,
		"Neighbor": Neighbor,
		"Price": Price,
		"Bedrooms": Bedrooms,
		"Bathrooms": Bathrooms,
		"isSale": $('#AdSearchSlae').hasClass("ui-btn-active")},
		function(data){
			$("#ListResult").html(data);
			$('.ViewDetailBlock').click(ViewDetail); 
		});
	}
			
	function PreNextBtn(e)
	{
		var strLocation = $("#searchinput3").val();
		$.post( strWebSite + 'SearchProperties2.php',
		{"strLocation": strLocation,
		"Caller": $(this).attr("id")},
		function(data){
			$("#ListResult").html(data);
			$('.ViewDetailBlock').click(ViewDetail); 
//			$('#P10PreBtn').click(LocationSearch2); 
//			$('#P10NextBtn').click(LocationSearch2); 
		});
	}	

	//---------------View Detail
	function ViewDetail(e){
		var idProperty = $(this).attr("id");
		$.post( strWebSite + 'PropertyDetails.php',
			{"arg": idProperty},
			function(data){
				$("#PropertiesDetail").html(data);
			});
		$.post(strWebSite + 'PropertyMap.php',
			//{"arg": idProperty},
			function(data){
				$("#Map").html(data);
			});
	}

	function ViewMapResults(e){
		$.post(strWebSite + 'PropertyMap.php',
			//{"arg": idProperty},
			function(data){
				$("#Map").html(data);
			});
	}
	function ViewBrokerMap(e)
	{
		$.post( strWebSite + 'BrokerMap.php',
		function(data){
			$("#Map").html(data);
		});		
	}
	
	
//	function ViewDetail(e)
//	{
//		var idProperty = $(this).attr("id");
//		$.post(strWebSite + 'AgentDetails.php',
//		function(data){
//			$("#ViewAgentDetail").html(data);
//		});
//		$.post(strWebSite+'PropertyMap.php',
//		//{"arg": idProperty},
//		function(data){
//			$("#Map").html(data);
//		});
//	}
	
/*
			//---------------Zip Code Search
			$("#Page6Search").click(function(){
				var strLocation = $("#searchinput3").val();
				$.post('SearchProperties2.php',
				{"arg": strLocation},
				function(data){
					$("#ListResult").html(data);
				});
			});
*/			

			
			$("#Page10Detail").click(function(){
				var idProperty = $("#Property").val();
				$.post('PropertyDetails.php',
				{"arg": idProperty},
				function(data){
					$("#PropertiesDetail").html(data);
				});
				$.post('PropertyMap.php',
				//{"arg": idProperty},
				function(data){
					$("#Map").html(data);
				});
			});
			//---------------View Detail
			$(".PropertyDetail").click(function(){
				//var idProperty = $(".Property").val();
				$.post('PropertyDetails.php',
				//{"arg": idProperty},
				function(data){
					$("#PropertiesDetail").html(data);
				});
				//$.post('PropertyMap.php',
				//{"arg": idProperty},
				//function(data){
				//	$("#Map").html(data);
				//});
			});
			
			
	//daria			
	function searchSpaceSize(){
		var orient;

		if (window.orientation) { 
			orient = Math.abs(window.orientation) === 90 ? 'landscape' : 'portrait';
		}
		else if (window.screen) {
			var width = screen.width;
			var height = screen.height;
			orient = (width > height) ? 'landscape' : 'portrait';
		}
		else {
			orient = 'unknow';
		}		

		var spaceheight=0;
		var spacewidth=0;
		var listwidth='100%';
		var objresultlistCount=0;
		var appDetailHeight=560;
		var appDetailWidth=970;
		
		if(isMap=='y'){
			objresultlistCount=$('.resultlist').find('li').length;
		}
		if(orient=='landscape'){
			if(isMap=='y' && objresultlistCount>2){
				spaceheight=objresultlistCount*290;
			}else{
				spaceheight=400;
			}
			spacewidth=630;
			
			$('#appDetail').css({
				height : appDetailHeight,
				width : appDetailWidth
			});
			$('#appDetail .btns').css({
				left : 60,
				top : 100
			});
			$('#appDetail .mapTitleSpace').css({
				height : 25
			});
		}else{
			if(isMap=='y' && objresultlistCount>2){
				listwidth=objresultlistCount*340;
			}
			spaceheight=400;
			spacewidth=listwidth;

			$('#appDetail').css({
				height : appDetailWidth-120,
				width : appDetailHeight+100
			});
			$('#appDetail .btns').css({
				left : 260,
				top : -240
			});
			$('#appDetail .mapTitleSpace').css({
				height : 0
			});
		}
		$('#resultmapspace').css({
			height : spaceheight,
			width : spacewidth
		});
		$('#resultlist').css({
			width : listwidth
		});
		$('#page14').css({
			width : listwidth
		});
	}

	window.addEventListener('load', searchSpaceSize, false);
	// listening orientationchange event
	window.addEventListener('orientationchange', searchSpaceSize, false);
})(jQuery);

